package com.wangyadong.hobby.thread;

import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.stream.IntStream;

/**
 * StuffIntoPublic
 * <p/>
 * Unsafe publication
 *
 * @author Brian Goetz and Tim Peierls
 */
public class StuffIntoPublic {
    public volatile Holder holder;

    public void initialize(int i) {
        holder = new Holder(i);
    }



    public static void main(String[] args) {
        StuffIntoPublic stuffIntoPublic = new StuffIntoPublic();
        Set emptySet = new HashSet();
        Set emptySet1 = new HashSet();
        IntStream.range(0, 30000).forEach(i -> {
            new Thread(() -> {
                stuffIntoPublic.initialize(i);
                Holder holder = stuffIntoPublic.holder;
                holder.assertSanity();
                emptySet1.add(i);
                emptySet.add(holder.hashCode());
            }).start();
        });
        System.out.println("--" +emptySet.size());
        System.out.println("==" +emptySet1.size());
    }
}